
import ElementUI from 'element-ui'
import axios from 'axios'
import router from './router'
import { Promise } from 'core-js'



// axios.defaults.baseURL = "http://localhost:8081"          //配置baseURL

const request = axios.create({
    timeout: 5000,           //配置延时器
    headers: {
        "Content-Type": "application/jion;charset='utf-8'",
    }
})

//请求拦截
request.interceptors.request.use(config => {
    config.headers['Aurhorization'] = localStorage.getItem("token")
    return config
})

//返回拦截
request.interceptors.response.use(response => {
       
    let data = response.data
    
    if (data.code === 200) {
      
        return response
    } else {
        ElementUI.Message.error(!data.msg ? "系统异常" : data.msg)
        return Promise.reject(data.data.msg)
    }
}
,

error => {
   

if (error.response.data){
    error.message = error.response.data.msg
    console.log(error)
}

    if (error.response.status === 401) {
        router.push("/login")
    }
    ElementUI.Message.error(error.message, { duration: 3000 })
    return Promise.reject(error)
}
)

export default request